//
//  HomeVC.m
//  YuDou
//
//  Created by yingqi on 2018/6/22.
//  Copyright © 2018年 yingqi. All rights reserved.
//

#import "HomeVC.h"

#import "PageTitleView.h"
#import "PageContentView.h"

#import "RecommendVC.h"
#import "GameVC.h"
#import "AmuseVC.h"
#import "HandsVC.h"
#import "FunnyVC.h"

@interface HomeVC ()<PageTitleViewDelegate,PageContentViewDelegate>
@property (nonatomic, strong) PageTitleView *pageTitleView;
@property (nonatomic, strong) PageContentView *pageContentView;
@property (nonatomic, strong) NSMutableArray <UIViewController *>*childVcs;
@end

@implementation HomeVC

-(NSMutableArray<UIViewController *> *)childVcs
{
    if (!_childVcs) {
        _childVcs = [NSMutableArray array];
        [_childVcs addObject:[RecommendVC new]];
        [_childVcs addObject:[GameVC new]];
        [_childVcs addObject:[AmuseVC new]];
        [_childVcs addObject:[HandsVC new]];
        [_childVcs addObject:[FunnyVC new]];
    }
    return _childVcs;
}

- (PageContentView *)pageContentView
{
    if (!_pageContentView) {
        CGRect contentFrame = CGRectMake(0,  kStatusBarH + kNavBarH + kTitleViewH,  kScreenWidth, kScreenHeight - kStatusBarH - kNavBarH - kTitleViewH - kTabBarH);
        _pageContentView = [[PageContentView alloc] initWithFrame:contentFrame childVcs:self.childVcs parentVc:self];
        _pageContentView.delegate = self;
    }
    
    return _pageContentView;
}

- (PageTitleView *)pageTitleView
{
    if (!_pageTitleView) {
        NSArray *titles = @[@"推荐",@"手游",@"娱乐",@"游戏",@"趣玩"];
        CGRect titleFrame = CGRectMake(0, kStatusBarH + kNavBarH, kScreenWidth, kTitleViewH);
        _pageTitleView = [[PageTitleView alloc] initWithFrame:titleFrame titles:titles];
        _pageTitleView.delegate = self;
    }
    return _pageTitleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI
{
    [self setupNavBar];
    [self.view addSubview:self.pageTitleView];
    [self.view addSubview:self.pageContentView];
}

- (void)setupNavBar
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"homeLogoIcon" highlightedImage:nil size:CGSizeZero];
    CGSize size=CGSizeMake(40, 40);
    UIBarButtonItem *searchItem = [UIBarButtonItem itemWithImageName:@"searchBtnIcon" highlightedImage:@"searchBtnIconHL" size:size];
    UIBarButtonItem *qrItem =  [UIBarButtonItem itemWithImageName:@"scanIcon" highlightedImage:@"scanIconHL" size:size];
    UIBarButtonItem *historyItem = [UIBarButtonItem itemWithImageName:@"viewHistoryIcon" highlightedImage:@"viewHistoryIconHL" size:size];
    UIBarButtonItem *MessageItem = [UIBarButtonItem itemWithImageName:@"siteMessageHome" highlightedImage:@"siteMessageHomeH" size:size];
    self.navigationItem.rightBarButtonItems = @[searchItem,qrItem,historyItem,MessageItem];
}

#pragma mark - PageContentViewDelegate
- (void)pageContentView:(PageContentView *)pageContentView progress:(CGFloat)progress sourceIndex:(NSInteger)sourceIndex targetIndex:(NSInteger)targetIndex
{
    [self.pageTitleView setTitleWithProgress:progress sourceIndex:sourceIndex targetIndex:targetIndex];
}

#pragma mark - PageTitleViewDelegate
- (void)pageTitleView:(PageTitleView *)titleView selectedIndex:(NSInteger)selectedIndex
{
    [self.pageContentView setCurrentIndex:selectedIndex];
}
@end
